package com.lire.shop.service.impl;

import com.lire.shop.dao.WalletRepository;
import com.lire.shop.data.NullObjects;
import com.lire.shop.entity.Wallet;
import com.lire.shop.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;

@Service
public class WalletServiceImpl implements WalletService {

    @Autowired
    private WalletRepository walletRepository;

    @Override
    public BigDecimal getBalances(Long wid) {
        Optional<Wallet> walletOptional = walletRepository.findById(wid);
        Wallet wallet = walletOptional.orElseGet(NullObjects::getWallet);
        return wallet.getBalances();
    }

    @Override
    public Wallet getWalletById(Long wid) {

        return walletRepository.findById(wid)
                .orElseGet(NullObjects::getWallet);
    }

    @Override
    public void updateBalances(Wallet wallet) {
       walletRepository.updateBalancesByWid(wallet.getBalances(),wallet.getWid());
    }


}
